###################################################################################################
# Prohibition Referenda MetaData ##################################################################
# Michael Olson and James Snyder ##################################################################
###################################################################################################

###################################################################################################
# referenda votes ##################################################################################
###################################################################################################

# prohibition data

  meta_forsg <- read.csv("./prohib_votes_long_pretty.csv")
  
  # make a more uniform set of topics
  
    meta_forsg$clean_topic <- NA
  
    meta_forsg[is.element(meta_forsg$topic_prohib,c("Statewide prohbition",
                                      "End statewide prohibition",
                                      "State prohibition",
                                      "State Prohibition",
                                      "Institute statewide prohibition",
                                      "Vote for Prohibition in Constitution",
                                      "Bone-dry prohibition",
                                      "Definition of liquor and prohibition",
                                      "Prohibition Amendment",
                                      "State prohibition constitutional amendment",
                                      "Constitutional amendment for prohibition",
                                      "Referendum for Statewide Prohibition",
                                      "Repeal statewide prohibition",
                                      "Statewide prohibition",
                                      "Repeal state prohibition",
                                      "Repeal Prohibition",
                                      "Repeal prohibition",
                                      "Repeal of statewide prohibition",
                                      "Repeal state prohibition laws",
                                      "Repeal of State constitutional Prohibition",
                                      "Allow liquor",
                                      "Legalize Alcohol",
                                      "Hobart Prohibition Act",
                                      "Repeal statutory prohibition",
                                      "Repeal Liquor Law",
                                      "Amend Constitution", # colorado 1926 to allow for production and sale intrastate
                                      "Amend state constitution", # colorado 1932 repeal state prohibition subject to federal repeal
                                      "Const. Amend for Prohibition", # Alabama 1909 prohibit manufacture and sale of alcohol
                                      "Allow sale of liquor")),"clean_topic"] <- "State Prohibition"

    meta_forsg[is.element(meta_forsg$topic_prohib,c("Ratify 18th Am.",
                                      "21th Amendment",
                                      "Ratify 21st Am.",
                                      "Convention for 21st Am.",
                                      "Repeal of 18th Am.",
                                      "Delegates to Convention for 21st Am.",
                                      "Delegates for 21st Am. Convention",
                                      "Convention Delegates Election for Repeal",
                                      "Petition to call Convention for Repeal of 18th Am.",
                                      "Ratification of 21st Am.",
                                      "Repeal of federal prohibition",
                                      "Modify 18th Am.",
                                      "Ratify 21st Am.",
                                      "Repeal 18th Am.",
                                      "Enforce Federal Prohibition",
                                      "Constitutional Convention Delegates Election", # missouri 1933 election
                                      "Constitutional Convention election", # kentucky 1933 election
                                      "Congressional memorial about Volstead Act")),"clean_topic"] <- "Federal Prohibition"

    meta_forsg[is.element(meta_forsg$topic_prohib,c("Local option and homemade liquor",
                                      "Local Option Elections on Alcohol Prohibition",
                                      "Local Option",
                                      "Local option",
                                      "Local option for light wine production",
                                      "Local Option liquor control",
                                      "County Prohibition",
                                      "Repeal county option")),"clean_topic"] <- "Local Option"

    meta_forsg[is.element(meta_forsg$topic_prohib,c("Beer exception to prohibition",
                                      "Define liquor",
                                      "Relax prohibition regulations",
                                      "Statewide prohibition, except wine",
                                      "Definition of liquor and prohibition in public" ,
                                      "Allow low-ABV beer",
                                      "Legalization of low-ABV malt liquor",
                                      "Amend state prohibition",
                                      "Allow light wines and beers",
                                      "Legalize sale of beer",
                                      "Definition of liquor",
                                      "Provide for licensing and taxation of beer",
                                      "Legalize sale of beer")),"clean_topic"] <- "Exceptions"

    meta_forsg[is.element(meta_forsg$topic_prohib,c("Relating to liquor transportation",
                                      "Prohibition of liquor trafficking",
                                      "Prohibit alcohol trade",
                                      "Forbid importation of liquor",
                                      "Define and establish penalties for bootlegging")),"clean_topic"] <- "Transportation"

    meta_forsg[is.element(meta_forsg$topic_prohib,c("Forbid manufacture and sale of liquor",
                                      "Prohibit production of liquor",
                                      "Search and seizure of alcohol",
                                      "Repeal creation of Alcholic Beverage Commission",
                                      "Statewide prohibition of liquor trafficking" )),"clean_topic"] <- "State Prohibition"

    meta_forsg[is.element(meta_forsg$topic_prohib,c("Enforcement of Prohibition",
                                      "Prohibition Enforcement",
                                      "Prohibition enforcement",
                                      "Suspension of prohibition enforcement",
                                      "Amend prohibition enforcement",
                                      "Repeal prohibition enforcement",
                                      "Repeal prohibition enforcement",
                                      "Repeal Enforcement Act")),"clean_topic"] <- "Enforcement"

    meta_forsg[is.element(meta_forsg$topic_prohib,c("Liquor permit issuance",
                                      "Provides licenses for liquor",
                                      "Liquor licensing")),"clean_topic"] <- "State Prohibition"

    meta_forsg[is.element(meta_forsg$topic_prohib,c("Prohibit ballot questions on alcohol for 8 years",
                                      "When to enact statewide prohibition")),"clean_topic"] <- "Unknown"

    meta_forsg[is.element(meta_forsg$topic_prohib,c("Legislative resolution in opposition",
                                                    "Call constitutional convention", # Nevada 1926 -- not sure
                                      "xxx")),"clean_topic"] <- "Unknown"

    
# fix up the detailed topics to be more consistent
    
    # make a more uniform set of topics
    
    meta_forsg$topic_prohib <- as.character(meta_forsg$topic_prohib)
    
    meta_forsg[is.element(meta_forsg$topic_prohib,c("Statewide prohbition",
                                                    "State prohibition",
                                                    "State Prohibition",
                                                    "Institute statewide prohibition",
                                                    "Vote for Prohibition in Constitution",
                                                    "Bone-dry prohibition",
                                                    "Definition of liquor and prohibition",
                                                    "Prohibition Amendment",
                                                    "State prohibition constitutional amendment",
                                                    "Constitutional amendment for prohibition",
                                                    "Referendum for Statewide Prohibition",
                                                    "Statewide prohibition",
                                                    "Hobart Prohibition Act",
                                                    "Const. Amend for Prohibition" # Alabama 1909 prohibit manufacture and sale of alcohol
                                                    )),"topic_prohib"] <- "State Prohibition"
    
    meta_forsg[is.element(meta_forsg$topic_prohib,c("End statewide prohibition",
                                                    "Repeal statewide prohibition",
                                                    "Repeal state prohibition",
                                                    "Repeal Prohibition",
                                                    "Repeal prohibition",
                                                    "Repeal of statewide prohibition",
                                                    "Repeal state prohibition laws",
                                                    "Repeal of State constitutional Prohibition")),"topic_prohib"] <- "Repeal State Prohibition"
    
    meta_forsg2 <- meta_forsg[(meta_forsg$year>=1910 & meta_forsg$year<=1920) | (meta_forsg$year>=1928 & meta_forsg$year<=1935),]
    
    # descriptives for text #######################################################################################################
    
    meta_forsg2$ref_id <- paste(meta_forsg2$state,meta_forsg2$referendum_number)

    # number of referenda
    
      fileConn<-file("./results/n_referenda.tex")
      writeLines(format(round(length(unique(meta_forsg2$ref_id)),2),0), fileConn)
      close(fileConn)
    
    # number of states
    
      fileConn<-file("./results/n_states.tex")
      writeLines(format(round(length(unique(meta_forsg2$state)),2),0), fileConn)
      close(fileConn)
    
    # number of counties
    
      fileConn<-file("./results/n_counties.tex")
      writeLines(format(round(length(unique(meta_forsg2$areaname)),2),0), fileConn)
      close(fileConn)
    
    # total population of covered counties
      
      # fileConn<-file("./results/pop_share.tex")
      # writeLines(paste(format(round((sum(meta_forsg2[!duplicated(meta_forsg2$areaname),"pop20"],na.rm=TRUE)/106500000)*100,0),0),"%",sep=""), fileConn)
      # close(fileConn)

# create county-level pro-prohibition share
    
    meta_forsg2$pro_pct <- meta_forsg2$yes_prohib/(meta_forsg2$yes_prohib+meta_forsg2$no_prohib)
    
##############################################################################################################################    
    
# overall
    
    cors <- list()
    
    for(j in 1:length(unique(meta_forsg2$state))){
      
        bit <- meta_forsg2[meta_forsg2$state==sort(unique(meta_forsg2$state))[j],]
        bit$areaname <- as.character(bit$areaname)
        
        bit <- bit[,c("areaname","referendum_number","pro_pct")] %>% spread(referendum_number,pro_pct)

        bit <- bit[,is.element(colnames(bit),1:30)]

        cors[[j]] <- cor(as.matrix(bit),use="pairwise.complete.obs")[lower.tri(cor(as.matrix(bit),use="pairwise.complete.obs"))]

    }
    
    fileConn<-file("./results/overall_correlation.tex")
    writeLines(format(round(mean(unlist(cors)),2),nsmall=2),fileConn)
    close(fileConn)

# correlation matrices
        
    early <- meta_forsg2[meta_forsg2$year<=1920,]
    
    late  <- meta_forsg2[meta_forsg2$year>=1928,]
    
    # for each, collapse within broader topic, then spread
    
    early <- spread(aggregate(pro_pct~clean_topic+state+county,data=early,FUN=mean),clean_topic,pro_pct)
        
    late <- spread(aggregate(pro_pct~clean_topic+state+county,data=late,FUN=mean),clean_topic,pro_pct)
    
    # now find correlations
    
    early_matrix <- apply(cor(early[,which(!is.element(colnames(early),c("state","county","Unknown")))],use="pairwise.complete.obs"),1,function(x) as.character(round(x,3)))
      early_matrix[lower.tri(early_matrix)==F] <- ""
      early_matrix <- as.data.frame(early_matrix); rownames(early_matrix) <- colnames(early_matrix); early_matrix <- early_matrix[-1,-ncol(early_matrix)]
      early_sg <- stargazer(early_matrix,summary=F,rownames = T,colnames=T,
                title = "Correlations between Different Categories of Referendums, Early Period (1910-1920)",label="early_cor_matrix",font.size="footnotesize",
                notes = "\\parbox[t]{0.975\\textwidth}{\\scriptsize \\textit{Note}: Table presents within-county correlations for different types of referendums. If a county had multiple
                referendums in one category, the mean of those is used in calculating the correlation. Cell entries represent correlation among all counties that had at least
                one of both types of referendums, so the number of observations differs across cells.}")
      early_sg <- gsub("cccc","lccc",fixed=T,early_sg)
      cat(early_sg, sep = '\n', file  = paste("./results/early_cor_matrix.tex",sep=""))
      
    late_matrix <- apply(cor(late[,which(!is.element(colnames(late),c("state","county","Unknown")))],use="pairwise.complete.obs"),1,function(x) as.character(round(x,3)))
      late_matrix[lower.tri(late_matrix)==F |is.na(late_matrix)] <- ""    
      late_matrix <- as.data.frame(late_matrix); rownames(late_matrix) <- colnames(late_matrix); late_matrix <- late_matrix[-1,-ncol(late_matrix)]
      late_sg     <- stargazer(late_matrix,summary=F,rownames = T,colnames=T,
                title = "Correlations between Different Categories of Referendums, Late Period (1928-1935)",label="late_cor_matrix",font.size="footnotesize",
                notes = "\\parbox[t]{0.8\\textwidth}{\\scriptsize \\textit{Note}: Table presents within-county correlations for different types of referendums. If a county had multiple
                referendums in one category, the mean of those is used in calculating the correlation. Cell entries represent correlation among all counties that had at least
                one of both types of referendums, so the number of observations differs across cells.}")
      late_sg <- gsub("cccc","lccc",fixed=T,late_sg)
      cat(late_sg, sep = '\n', file  = paste("./results/late_cor_matrix.tex",sep=""))
      
  # aggregate to the state level  
  
  meta_forsg <- as.data.frame(meta_forsg %>% group_by(state,year,topic_prohib,clean_topic,referendum_number) %>% summarise(pro_prohibition_share = 
                                                                                                                 100*(sum(yes_prohib)/(sum(yes_prohib)+sum(no_prohib)))))
  
  meta_forsg <- meta_forsg[,c("state","year","pro_prohibition_share","clean_topic","topic_prohib")]
  meta_forsg$pro_prohibition_share <- paste(as.character(round(meta_forsg$pro_prohibition_share,1)),"%",sep="")
  
  colnames(meta_forsg) <- c("State","Year","Prohibition %","Broad Topic", "Specific Topic")
  
  meta_forsg <- dplyr::arrange(meta_forsg,State,Year,`Specific Topic`)
  
  meta_forsg$Year <- as.character(meta_forsg$Year)
  meta_forsg[meta_forsg$`Specific Topic`=="xxx","Specific Topic"] <- ""
  
# do something crazy
      
      meta_forsg <- cbind(meta_forsg[1:(nrow(meta_forsg)/2),],
                          meta_forsg[((nrow(meta_forsg)/2)+1):nrow(meta_forsg),])

  tab <- stargazer(meta_forsg,
            summary=FALSE,
            rownames = FALSE,
            no.space = TRUE,
            font.size = "tiny",
            title = "State Prohibition Referenda",
            label = "referenda",
            column.sep.width = "0pt")
  
  collapse <- function(st) paste(st, collapse="")
  
  st <- gsub("cccccccccc","lllll|lllll",fixed=T,tab)
  st <- gsub(".1","",fixed=TRUE,st)
  st <- gsub("tiny","notsotiny",fixed=TRUE,st)
  cat(st, sep = '\n', file = "./results/referendum_info_new.tex")
  

  
  